/**
 * 自定义ueditor指令
 */
define(function (require, exports, module) {
    require.async('./wx-editor.css');

    var angular = require('angular');

    var module = angular.module('direcitves.ueditor',[]);
    module.directive('ueditor',['$timeout',
        function ($timeout) {
            return{
                restrict: 'EA',
                require: 'ngModel',
                scope: {
                    height: '@?'
                },
                link: function (scope, element, attr, ctrl) {
                    var _self = this,
                        _initContent,
                        editor,
                        editorReady = false;

                    var fexUE = {
                        initEditor: function () {
                            var _self = this;

                            //自定义按钮
                            'more,wx-pic,wx-sound,wx-video,wx-link'.replace(/[^, ]+/g,function(cmd){
                                var lang={
                                    'more':'更多',
                                    'wx-pic':'图片'
                                };

                                UE.ui[cmd]=function (editor) {
                                    var ui = new UE.ui.Button({
                                        className:'edui-for-' + cmd,
                                        title:lang[cmd],
                                        name:lang[cmd],
                                        onclick:function () {
                                            editor.execCommand(cmd);
                                        },
                                        theme:editor.options.theme,
                                        showText:false
                                    });
                                    UE.ui.buttons[cmd] = ui;
                                    return ui;
                                };
                            });
                            editor = new UE.ui.Editor({
                                initialContent: _initContent,
                                toolbars:[
                                    ['more','|','fontsize','|','blockquote','|','horizontal','|','removeformat','|','link','unlink','|','wx-pic','fullscreen'],
                                    ['bold','italic','underline','forecolor','backcolor','|','justifyleft','justifycenter','justifyright','|','rowspacingtop','rowspacingbottom','lineheight','|','insertorderedlist','insertunorderedlist','|','imagenone','imageleft','imageright','imagecenter']
                                ],
                                initialFrameHeight:scope.height || 120,
                                autoHeightEnabled:false,
                                wordCount:false,
                                elementPathEnabled: false
                            });

                            editor.render(element[0]);
                            editor.ready(function () {
                                editorReady = true;
                                _self.setContent(_initContent);

                                editor.addListener('contentChange', function () {
                                    scope.$apply(function () {
                                        ctrl.$setViewValue(editor.getContent());
                                    });
                                });
                            });
                            editor.registerCommand('more', {
                                execCommand: function() {
                                    //$(this.ui.getDom("toolbarbox")).toggleClass('not-show-more');
                                }
                            });
                            editor.registerCommand('wx-pic', {
                                execCommand: function() {
                                    $('#ueditor_image').trigger('click');
                                    //SelectorPlugin.show(function(data){
                                    //    // normal is ready
                                    //    var imgHtml=[];
                                    //    for(var i=0;i<data.length;i++){
                                    //        imgHtml.push('<img src="'+data[i]['wechat_url']+'">');
                                    //    }
                                    //    msgEditor.setContent(imgHtml.join(''),true);
                                    //},true);
                                }
                            });
                        },
                        setContent: function (content) {
                            if (editor && editorReady) {
                                editor.setContent(content);
                            }
                        }
                    };

                    /**
                     * 当Model改变值得时候赋值。
                     */
                    ctrl.$render = function () {
                        _initContent = ctrl.$isEmpty(ctrl.$viewValue) ? '' : ctrl.$viewValue;
                        fexUE.setContent(_initContent);
                    };

                    scope.$on('addImage',function(event,data){
                        $timeout(function(){
                            editor.setContent('<img src="'+data.img_url+'">',true);
                        },0);

                    })

                    seajs.use(['/static_public/js/lib/ueditor/ueditor.config',
                        '/static_public/js/lib/ueditor/ueditor.all',
                        '/static_public/js/lib/ueditor/lang/zh-cn/zh-cn'],function(){
                        fexUE.initEditor();
                    });
                }
            }
        }]
    )

})